local filename = "realnameauth.lua"

local timetool = require "timetool"
local skynet = require "skynet"
local cluster = require "skynet.cluster"
local filelog =require "filelog"
require "pubenum"

local service = nil
local CMD = {
	service = nil
}

--
--实名认证消息处理
--必要参数：idcardnum身份证号 realname真实姓名
--
function CMD.process(rid, msgbody)
	if not service then
		service = CMD.service
	end

    --判断是否认证过
    if tonumber(service.info.idtrue) == 1 then
         return {retcode = EErrCode.ERR_INVALID_REQUEST,retstr = "已经实名认证过了"}
    end

    --验证参数是否合法
	local idcardnum = msgbody.idcardnum or ""
    local realname = msgbody.realname or ""
    if string.len(idcardnum) ~= 18 or  string.len(realname) < 1 then
        return {retcode = EErrCode.ERR_INVALID_REQUEST,retstr = "身份证号码或名字格式错误"}
    end

    local fisrt = tonumber(string.sub(idcardnum,1,1)) or 0
    local high = tonumber(string.sub(idcardnum,2,6))
    local year = tonumber(string.sub(idcardnum,7,10)) or 0
    local month = tonumber(string.sub(idcardnum,11,12)) or 0
    local day = tonumber(string.sub(idcardnum,13,14)) or 0
    local rnum = tonumber(string.sub(idcardnum,15,17))
    local low = string.sub(idcardnum,18,18)
	if low == 'x' or low == 'X' then low = '0' end
	low = string.byte(low)
    if fisrt == 0 or not high or not rnum or low < string.byte('0') or low > string.byte('9') then
        return {retcode = EErrCode.ERR_INVALID_REQUEST,retstr = "错误的身份证号码"}
    end

    --判断日期是否正确
    local curdata = os.date("*t",timetool.get_time())
    local months = {[1] = 31,[3] = 31,[5] = 31,[7] = 31,[8] = 31,[10] = 31,[12] = 31}
    if year < 1900 or year > curdata.year or month < 1 or month > 12 or day < 1 or day > 31 then
        return {retcode = EErrCode.ERR_INVALID_REQUEST,retstr = "错误的身份证号码"}
    end

    local is_ok = false
    if day == 29 and (year % 4 == 0 and year % 100 ~= 0 or year % 400 == 0) then 
        is_ok = true
    elseif day == 31 and months[month] then 
        is_ok = true 
    else 
        is_ok = true 
    end
    if not is_ok then
        return {retcode = EErrCode.ERR_INVALID_REQUEST,retstr = "错误的身份证号码"}
    end

    --检验校验位
    local sum = 0
    local xishu = {[1] = 7,[2] = 9, [3] = 10,[4] = 5,[5] = 8,[6] = 4,[7] =2,[8] =1,
    [9] = 6,[10] = 3,[11] = 7,[12] =9,[13] = 10,[14] = 5,[15] = 8,[16] = 4,[17] =2}
    local mods = {[0] = 1,[1] = 0,[2] = 'X',[3] = 9,[4] = 8,[5] = 7,[6] = 6,[7] = 5,[8] = 4, [9] = 3, [10] = 2}
    for i = 1,17 do
        local cur_num = string.sub(idcardnum,i,i)
        sum = sum + cur_num * xishu[i]
    end
    local mod = string.sub(idcardnum,18,18)
    if mod == 'x' or mod == 'X' then
        mod = 'X'
    else
        mod = tonumber(mod)
    end
    if mod ~= mods[sum % 11] then
        return {retcode = EErrCode.ERR_INVALID_REQUEST,retstr = "错误的身份证号码"}
    end

    local info = {idcardtype = 1,idcardnum = idcardnum,realname = realname,idtrue = 1}
	local result = cluster.call("datanode", ".router", "update_rk", rid, "rs_info", nil,info)

    --更新数据
    service.info.idtrue = 1
    service.info.idcardnum = idcardnum
    service.info.realname = realname

	return { }
end

return CMD